package com.fmx.BookStore.controller;


import com.fmx.BookStore.core.domain.AjaxResult;
import com.fmx.BookStore.domain.Permission;
import com.fmx.BookStore.domain.User;
import com.fmx.BookStore.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    IPermissionService permissionService;

    @GetMapping(value = "/{permissionId}")
    @ApiOperation("根据ID找权限")
    public AjaxResult selectPermissionById(@PathVariable("permissionId") Long permissionId){
        return AjaxResult.success(permissionService.selectPermissionById(permissionId));
    }
    @GetMapping
    @ApiOperation("根据其他信息查询权限")
    public AjaxResult listPermission(@RequestParam(defaultValue = "") String permissionName){
        List<Permission> list = permissionService.findMenus(permissionName);
        return AjaxResult.success(list);
    }

    @PostMapping
    @ApiOperation("新增权限")
    public AjaxResult addPermission(@RequestBody Permission permission){
        int rows = permissionService.insertPermission(permission);
        return rows > 0 ? AjaxResult.success("添加权限成功") : AjaxResult.error("添加权限失败");
    }

    @PutMapping
    @ApiOperation("修改权限信息")
    public AjaxResult updatePermission(@RequestBody Permission permission){
        int rows = permissionService.updatePermission(permission);
        return rows > 0 ? AjaxResult.success("修改权限成功") : AjaxResult.error("修改权限失败");
    }

    @DeleteMapping("/{permissionId}")
    @ApiOperation("删除权限")
    public AjaxResult deletePermission(@PathVariable Long permissionId){
        int rows = permissionService.deletePermission(permissionId);
        return rows > 0 ? AjaxResult.success("删除权限成功") : AjaxResult.error("删除权限失败");
    }

    @GetMapping("/page")
    @ApiOperation("分页操作")
    public AjaxResult page(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, Permission permission){
        PageHelper.startPage(pageNum,pageSize);
        List<Permission> list = permissionService.listPermission(permission);
        PageInfo<List> pageInfo = new PageInfo(list);
        return AjaxResult.success(pageInfo);
    }

}
